# WARNING! EXPERIMENTAL! Use setup.py if you're not familiar with cmake.
#
# We recommend that you create a separate directory for the build,
# so that the build files aren't mixed in with the source files.
# e.g.
#  $ mkdir build-cmake
#  $ cd build-cmake
#  $ cmake ..
#  $ make
#
# MacOSX Notes:
#   On MacOSX, it may default to 64-bit, even if your Python is 32-bit.
#   The linker will give NO WARNING, and the resulting .so file will
#   fail to load in Python. To fix this, run cmake as follows,
#   assuming you're in <source dir>/build-cmake/:
#     cmake -DCMAKE_OSX_ARCHITECTURES=i386 ..
#   To debug this issue, you can compare "lipo -info `which python`"
#   with "lipo -info numexpr.so". They should have the same platform info.
#
#   Further problems on OS X appear to be related to EPD Python. CMake's
#   default detection may be detecting the wrong python to link against.
#
# Windows Notes:
#   Python 2.7 is built with Visual C++ 9 (aka 2008). This is the one
#   you should pick when running cmake-gui. Be sure to switch the build
#   configuration from Debug to Release (or RelWithDebInfo) in Visual Studio.

project(numexpr)

cmake_minimum_required(VERSION 2.8)

# Force the default build type to be Release, because a Debug
# build doesn't work properly with the default Python build
if(NOT CMAKE_BUILD_TYPE)
  set(CMAKE_BUILD_TYPE "Release" CACHE STRING
      "Choose the type of build, options are: Debug Release
RelWithDebInfo MinSizeRel."
      FORCE)
endif()

set(CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR})

find_package(PythonInterp REQUIRED)
find_package(PythonLibsNew REQUIRED)
find_package(NumPy REQUIRED)

# Default install location for Python packages
if(CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT)
    set(CMAKE_INSTALL_PREFIX "${PYTHON_SITE_PACKAGES}" CACHE STRING
      "Choose the Python module directory (default site-packages)" FORCE)
endif()

# Require version >= 1.6
if(NUMPY_VERSION_DECIMAL LESS 10600)
    message(FATAL_ERROR,
        "NumExpr requires NumPy >= 1.6")
endif()

include_directories(
    ${PYTHON_INCLUDE_DIRS}
    ${NUMPY_INCLUDE_DIRS}
    )

set(numexpr_SRC
    numexpr/interpreter.cpp
    numexpr/module.cpp
    numexpr/numexpr_object.cpp
    numexpr/complex_functions.hpp
    numexpr/functions.hpp
    numexpr/interpreter.hpp
    numexpr/module.hpp
    numexpr/missing_posix_functions.hpp
    numexpr/msvc_function_stubs.hpp
    numexpr/numexpr_config.hpp
    numexpr/numexpr_object.hpp
    numexpr/opcodes.hpp
    )
if(CMAKE_HOST_WIN32)
    set(numexpr_SRC
        ${numexpr_SRC}
        numexpr/win32/pthread.c
        )
endif()

python_add_module(interpreter ${numexpr_SRC})

# Generate __config__.py. This is a dummy placeholder, as I
# don't know why it's here.
file(WRITE "${PROJECT_BINARY_DIR}/__config__.py"
    "# This file is generated by a CMakeFiles.txt configuration\n"
    "__all__ = ['get_info','show']\n"
    "def get_info(name):\n"
    "   return None\n"
    "def show():\n"
    "   print('someone called show()')\n")

# Install all the Python scripts
install(DIRECTORY numexpr DESTINATION "${CMAKE_INSTALL_PREFIX}"
    FILES_MATCHING PATTERN "*.py")
# Install __config__.py
install(FILES "${PROJECT_BINARY_DIR}/__config__.py"
    DESTINATION "${CMAKE_INSTALL_PREFIX}/numexpr")
# Install the module
install(TARGETS interpreter DESTINATION "${CMAKE_INSTALL_PREFIX}/numexpr")
